package com.css.openapi.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.css.common.core.constant.Result;
import com.css.common.core.constant.ReturnCode;
import com.css.common.core.exception.ServiceException;
import com.css.common.core.session.SessionUtils;
import com.css.common.core.utils.GyUtils;
import com.css.common.core.utils.bean.BeanUtils;
import com.css.openapi.BO.zrrdk.*;
import com.css.openapi.entity.ZfjkpZrrdkFpmxDO;
import com.css.openapi.entity.ZfjkpZrrdkHwlwmxDO;
import com.css.openapi.entity.ZfjkpZrrdkSfmxDO;
import com.css.openapi.entity.ZfjkpZrrdkWszm;
import com.css.openapi.feign.ZrrdkCommonClient;
import com.css.openapi.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ZrrdkServiceImpl implements ZrrdkService {

    @Resource
    private ZrrdkCommonClient zrrdkCommonClient;

    @Resource
    private ZfjkpZrrdkWszmService wszmService;

    @Resource
    private ZfjkpZrrdkFpmxService zfjkpZrrdkFpmxService;

    @Resource
    private ZfjkpZrrdkSfmxService zfjkpZrrdkSfmxService;

    @Resource
    private ZfjkpZrrdkHwlwmxService zfjkpZrrdkHwlwmxService;

    @Override
    public Result<LoginEwmBO> getLoginEwm(LoginEwmReq loginEwmReq) {
        LoginEwmResponse response =  zrrdkCommonClient.getLoginEwm(loginEwmReq.getProv());
        if(Objects.equals(response.getCode(),"0")){
            LoginEwmBO bo = new LoginEwmBO();
            bo.setQrcode(response.getQrcode());
            bo.setQrcodeid(response.getQrcodeid());
            return Result.ok(bo);
        }else {
            return Result.error(Integer.valueOf(response.getCode()),response.getMsg());
        }
    }

    @Override
    public Result<EwmResultBO> getEwmResult(EwmResultReq ewmResultReq) {

        EwmResultResponse response =  zrrdkCommonClient.getEwmResult(ewmResultReq.getProv(),ewmResultReq.getQrcodeid());
        if(Objects.equals(response.getCode(),"0")){
            EwmResultBO bo = new EwmResultBO();
            BeanUtils.copyProperties(response,bo);
            bo.setTaxUserId(response.getId());
            return Result.ok(bo);
        }else {
            return Result.error(Integer.valueOf(response.getCode()),response.getMsg());
        }
    }

    @Override
    public Result<SubmitDKBO> submitDK(SubmitDKReq submitDKReq) {

        SubmitDKResponse response =  zrrdkCommonClient.submitDK(submitDKReq.getProv(),submitDKReq.getTaxUserId(),submitDKReq.getLsh(),submitDKReq);
        if(Objects.equals(response.getCode(),"0")){
            SubmitDKBO bo = new SubmitDKBO();
            BeanUtils.copyProperties(response,bo);
            return Result.ok(bo);
        }else {
            return Result.error(Integer.valueOf(response.getCode()),response.getMsg());
        }
    }

    @Override
    public Result<SubmitAgainBO> submitAgain(SubmitAgainReq submitAgainReq) {

        SubmitAgainResponse response =  zrrdkCommonClient.submitAgain(submitAgainReq.getProv(),submitAgainReq.getTaxUserId(),submitAgainReq.getLsh());
        if(Objects.equals(response.getCode(),"0")){
            SubmitAgainBO bo = new SubmitAgainBO();
            BeanUtils.copyProperties(response,bo);
            return Result.ok(bo);
        }else {
            return Result.error(Integer.valueOf(response.getCode()),response.getMsg());
        }
    }


    @Override
    public Result<QueryInvoiceBO> queryInvoice(QueryInvoiceReq queryInvoiceReq) {

        QueryInvoiceResponse response =  zrrdkCommonClient.queryInvoice(queryInvoiceReq.getProv(),queryInvoiceReq.getTaxUserId(),queryInvoiceReq.getLsh(),queryInvoiceReq.getXzwszm());

        if(Objects.equals(response.getCode(),"0")){
            QueryInvoiceBO bo = new QueryInvoiceBO();
            BeanUtils.copyProperties(response,bo);
            //保存业务
            FpmxBO fpmx = bo.getFpmx();

            if(GyUtils.isNull(zfjkpZrrdkFpmxService.getById(fpmx.getSqduuid()))){
                ZfjkpZrrdkFpmxDO fpmxDO = new ZfjkpZrrdkFpmxDO();
                fpmxDO.setBz(fpmx.getBz());
                fpmxDO.setBzss(fpmx.getBzss());
                fpmxDO.setCjr(SessionUtils.getCjr());
                fpmxDO.setCjsj(new Date());
                fpmxDO.setFphm(fpmx.getFphm());
                fpmxDO.setKprq(fpmx.getKprq());
                fpmxDO.setSqduuid(fpmx.getSqduuid());
                fpmxDO.setYsxwfsd(fpmx.getYsxwfsd());
                double sumHwlwJe =  Double.parseDouble(fpmx.getSumHwlwJe());
                fpmxDO.setSumHwlwJe(BigDecimal.valueOf(sumHwlwJe));
                double sumHwlwSe = Double.parseDouble(fpmx.getSumHwlwSe());
                fpmxDO.setSumHwlwSe(BigDecimal.valueOf(sumHwlwSe));
                fpmxDO.setGrsdsZspmDm(fpmx.getGrsdsZspmDm());
                fpmxDO.setGrsdsZspmDm(fpmx.getGrsdsZspmDm());
                fpmxDO.setGhfdz(fpmx.getFpdkGmfxxVO().getGhfdz());
                fpmxDO.setGhflxdh(fpmx.getFpdkGmfxxVO().getGhflxdh());
                fpmxDO.setGhfnsrmc(fpmx.getFpdkGmfxxVO().getGhfnsrmc());
                fpmxDO.setGhfnsrsbh(fpmx.getFpdkGmfxxVO().getGhfnsrsbh());
                fpmxDO.setGhfyhyywdmc(fpmx.getFpdkGmfxxVO().getGhfyhyywdmc());
                fpmxDO.setGhfyhzh(fpmx.getFpdkGmfxxVO().getGhfyhzh());
                fpmxDO.setXhfdz(fpmx.getFpdkXsfxxVO().getXhfdz());
                fpmxDO.setXhflxdh(fpmx.getFpdkXsfxxVO().getXhflxdh());
                fpmxDO.setXhfnsrmc(fpmx.getFpdkXsfxxVO().getXhfnsrmc());
                fpmxDO.setXhfnsrsbh(fpmx.getFpdkXsfxxVO().getXhfnsrsbh());
                fpmxDO.setXhfyhyywdmc(fpmx.getFpdkXsfxxVO().getXhfyhyywdmc());
                fpmxDO.setXhfyhzh(fpmx.getFpdkXsfxxVO().getXhfyhzh());
                zfjkpZrrdkFpmxService.save(fpmxDO);
                List<FpdkSfmxGridlb> fpdkSfmxGridlbList =  fpmx.getFpdkSfmxGrid().getFpdkSfmxGridlb();

                for(FpdkSfmxGridlb gridlb:fpdkSfmxGridlbList){
                    ZfjkpZrrdkSfmxDO sfmxDO = new ZfjkpZrrdkSfmxDO();
                    BeanUtils.copyProperties(gridlb,sfmxDO);
                    sfmxDO.setCjsj(new Date());
                    sfmxDO.setCjr(SessionUtils.getCjr());
                    sfmxDO.setUuid(IdUtil.fastSimpleUUID());
                    sfmxDO.setFphm(fpmx.getFphm());
                    zfjkpZrrdkSfmxService.save(sfmxDO);
                }

                List<FpdkHwlwmxGridlb> fpdkHwlwmxGridlbList  = fpmx.getFpdkHwlwmxGrid().getFpdkHwlwmxGridlb();
                for(FpdkHwlwmxGridlb gridlb:fpdkHwlwmxGridlbList){
                    ZfjkpZrrdkHwlwmxDO zfjkpZrrdkHwlwmxDO = new ZfjkpZrrdkHwlwmxDO();
                    BeanUtils.copyProperties(gridlb,zfjkpZrrdkHwlwmxDO);
                    zfjkpZrrdkHwlwmxDO.setCjr(SessionUtils.getCjr());
                    zfjkpZrrdkHwlwmxDO.setCjsj(new Date());
                    zfjkpZrrdkHwlwmxDO.setFphm(fpmx.getFphm());
                    zfjkpZrrdkHwlwmxDO.setUuid(IdUtil.fastSimpleUUID());
                    zfjkpZrrdkHwlwmxService.save(zfjkpZrrdkHwlwmxDO);

                }

            }

            return Result.ok(bo);
        }else {
            return Result.error(Integer.valueOf(response.getCode()),response.getMsg());
        }
    }


    @Override
    public QrCodeRepsonse queryQrCode(QrCodeReq req) {
        QrCodeRepsonse response =  zrrdkCommonClient.queryQrCode(req.getProv(),req.getTaxUserId(),req.getLsh());
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        return response;
    }

    @Override
    public void uploadFlzl(FlzlReq req) {
        ReturnCode response =  zrrdkCommonClient.uploadFlzl(req.getProv(),req.getTaxUserId(),req);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
    }

    @Override
    @Transactional
    public List<WszmVO> queryWspzList(WszmReq req) {
        WszmRepsonse response =  zrrdkCommonClient.queryWspzList(req.getProv(),req.getTaxUserId(),req.getBdbz(),req.getCxrqq(),req.getCxrqz());
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        for (WszmVO wszmVO : response.getData()) {
            LambdaQueryWrapper<ZfjkpZrrdkWszm> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZfjkpZrrdkWszm::getTaxUserId,req.getTaxUserId())
                    .eq(ZfjkpZrrdkWszm::getDzsphm,wszmVO.getDzsphm());
            ZfjkpZrrdkWszm wszm = wszmService.getOne(queryWrapper);
            if (GyUtils.isNotNull(wszm)){
                wszm.setSjje(wszmVO.getSjje());
                wszm.setJkrq(wszmVO.getJkrq());
                wszm.setXgr(SessionUtils.getCjr());
                wszm.setXgsj(new Date());
                wszmService.updateById(wszm);
            }else {
                ZfjkpZrrdkWszm wszmDO = new ZfjkpZrrdkWszm();
                wszmDO.setUuid(GyUtils.getUuid());
                wszmDO.setJkrq(wszmVO.getJkrq());
                wszmDO.setDzsphm(wszmVO.getDzsphm());
                wszmDO.setProv(req.getProv());
                wszmDO.setTaxUserId(req.getTaxUserId());
                wszmDO.setSjje(wszmVO.getSjje());
                wszmDO.setCjr(SessionUtils.getCjr());
                wszmDO.setCjsj(new Date());
                wszmService.save(wszmDO);
            }
        }
        return response.getData();
    }

    @Override
    public String downloadWspz(DownloadWszmReq req) {
        DownloadWszmResponse response =  zrrdkCommonClient.downloadWspz(req.getProv(),req.getTaxUserId(),req.getBdbz(),req.getCxrqq(),req.getCxrqz(),req.getDzsphm());
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        return response.getData();
    }
}
